package net.blogjava.mobile.calendar;

import android.app.Activity;
import android.graphics.Canvas;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

public class CalendarView extends View {
	
	public Calendar ce;


	@Override
	protected void onDraw(Canvas canvas) {

		ce.draw(canvas);	// 向Calendar里添加Canvas

	}

	public CalendarView(Activity activity) {
		super(activity);
		ce = new Calendar(activity, this);		// 初始化 Calendar类, this指CalendarView对象
	}



	@Override
	public boolean onTouchEvent(MotionEvent motion) {		// 接触屏幕响应事件

		ce.grid.setCellX(motion.getX());	// 设置单元格的横坐标
		ce.grid.setCellY(motion.getY());
		if (ce.grid.inBoundary()) {
			this.invalidate();
		}
		return super.onTouchEvent(motion);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {		// 判断触摸屏幕按键

		switch (keyCode) {

			case KeyEvent.KEYCODE_DPAD_UP: {
				ce.grid.setCurrentRow(ce.grid.getCurrentRow() - 1);
				break;
			}
			case KeyEvent.KEYCODE_DPAD_DOWN: {
				ce.grid.setCurrentRow(ce.grid.getCurrentRow() + 1);
				break;
			}
			case KeyEvent.KEYCODE_DPAD_LEFT: {
				ce.grid.setCurrentCol(ce.grid.getCurrentCol() - 1);
				break;
			}
			case KeyEvent.KEYCODE_DPAD_RIGHT: {
				ce.grid.setCurrentCol(ce.grid.getCurrentCol() + 1);
				break;
			}
		
		}
		
		return true;
	}
}
